﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGenerator
{
    class CodeGenVisitor : AstNodeVisitor
    {
        private string result = "";

        public string GenerateCode(AsTree tree)
        {
            tree.GetRootNode().Accept(this);
            return result;
        }

        private void PushText(string str)
        {
            result += str;
        }

        override public bool Visit(AstMainClass node)
        {
            PushText(@"
	TITLE	" + node.ClassName.Value + @"
	.686P
	.XMM
	include listing.inc
	.model	flat

INCLUDELIB OLDNAMES
.code
EXTRN	__imp__printf:PROC
EXTRN	@__security_check_cookie@4:PROC
.CONST
$SG4294967291 DB '%d', 0aH, 00H
CONST	ENDS
");
            node.EntryFunction.Accept(this);

            PushText(@"
_TEXT	ENDS
END");

            return false;
        }

        public override bool Visit(AstEntryPointMainFunctionDecl node)
        {
            PushText(@"
PUBLIC	_main

_TEXT	SEGMENT
___formal$ = 8
___formal$ = 12
_main	PROC

	push	ebp
	mov	ebp, esp
"
            );

            node.Statements.Accept(this);

            PushText(@"
	xor	eax, eax

	pop	ebp
	ret	0
_main	ENDP
            ");
            return false;
        }

        public override bool Visit(AstConsoleWriteFunctionCall node)
        {
            PushText(@"
        push    " + node.IntParam.Value + @"  
        push    OFFSET $SG4294967291
        call    DWORD PTR __imp__printf
        add	    esp, 8
            ");

            return false;
        }

        override public bool Visit(AstStatements node)
        {
            return true;
        }

        override public bool Visit(AstMethodDecl node)
        {
            return false;
        }

        override public bool Visit(AstFunctionArgumentsList node)
        {
            return false;
        }

        override public bool Visit(AstVarDeclaration node)
        {
            return false;
        }

        override public bool Visit(AstAccessModifier node)
        {
            return false;
        }
        
        override public bool Visit(AstStaticModifier node)
        {
            return false;
        }

        public override bool Visit(AstArrayType node)
        {
            return false;
        }

        override public bool Visit(AstInteger node)
        {
            return false;
        }

        override public bool Visit(AstId node)
        {
            return false;
        }

        override public bool Visit(AstFunctionCall node)
        {
            return false;
        }

        override public bool Visit(AstFunctionCallWithReturnVal node)
        {
            return false;
        }

        override public bool Visit(AstArgumentsList node)
        {
            return false;
        }

        override public bool Visit(AstDotExpression node)
        {
            return false;
        }

        override public bool Visit(AstWhileStatement node)
        {
            return false;
        }

        override public bool Visit(AstArrayAccess node)
        {
            return false;
        }

        override public bool Visit(AstLogicalExpression node)
        {
            return false;
        }

        override public bool Visit(AstAddExpression node)
        {
            return false;
        }

        override public bool Visit(AstMinusExpression node)
        {
            return false;
        }

        override public bool Visit(AstMultiplicationExpression node)
        {
            return false;
        }

        override public bool Visit(AstDivisionExpression node)
        {
            return false;
        }

        override public bool Visit(AstAssigmentStatement node)
        {
            return false;
        }

        override public bool Visit(AstIfStatement node)
        {
            return false;
        }

        override public bool Visit(AstParenExpression node)
        {
            return false;
        }
    }
}
